﻿using System;
using System.ComponentModel;
using nRoute.Components.Composition;
using nRoute.Components.Disposer;
using nRoute.Components.Handlers;
using nRoute.Samples.OfficerXcel.Services;
using nRoute.SiteMaps;
using nRoute.Modules;

namespace nRoute.Samples.OfficerXcel
{
    [MapModule]
    public class WorksheetModule
        : IModule
    {
        private readonly IActiveWorksheetService _activeWorksheetService;
        private Handler<PropertyChangedEventArgs, PropertyChangedEventHandler> _propertyChangedHandler;
        private SiteMapNode _saveWorksheetNode;
        private SiteMapNode _saveWorksheetAsNode;
        private SiteMapNode _closeWorksheetNode;

        [ResolveConstructor]
        public WorksheetModule(IActiveWorksheetService activeWorksheetService)
        {
            if (activeWorksheetService == null) throw new ArgumentNullException("activeWorksheetService");
            _activeWorksheetService = activeWorksheetService;
        }

#region Implementation of IModule

        public void Initialize()
        {
            _activeWorksheetService.ActiveWorksheetChanged += (s, e) => SetupActiveWorksheetListeners();
            SetupSiteMapNodes();
        }

#endregion

#region Handlers

        private void Worksheet_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            _saveWorksheetNode.IsEnabled = _activeWorksheetService != null && _activeWorksheetService.ActiveWorksheet.IsDirty;
            _saveWorksheetAsNode.IsEnabled = _saveWorksheetNode.IsEnabled;
        }

#endregion

#region Setup Related

        private void SetupSiteMapNodes()
        {
            SiteMapService.LoadSiteMap((s) =>
            {
                _saveWorksheetNode = SiteMapService.ResolveSiteMapNode("SaveWorksheet");
                _saveWorksheetAsNode = SiteMapService.ResolveSiteMapNode("SaveWorksheetAs");
                _closeWorksheetNode = SiteMapService.ResolveSiteMapNode("CloseWorksheet");
                SetupActiveWorksheetListeners();
            });
        }

        private void SetupActiveWorksheetListeners()
        {
            if (_saveWorksheetNode == null) return;

            if (_activeWorksheetService.ActiveWorksheet == null)
            {
                _saveWorksheetNode.IsEnabled = false;
                _saveWorksheetAsNode.IsEnabled = false;
                _closeWorksheetNode.IsEnabled = false;
            }
            else
            {
                _saveWorksheetNode.IsEnabled = _activeWorksheetService.ActiveWorksheet.IsDirty;
                _saveWorksheetAsNode.IsEnabled = _saveWorksheetNode.IsEnabled;
                _closeWorksheetNode.IsEnabled = true;

                if (_propertyChangedHandler != null) _propertyChangedHandler.Dispose();
                var _worksheet = _activeWorksheetService.ActiveWorksheet;
                _propertyChangedHandler = new Handler<PropertyChangedEventArgs, PropertyChangedEventHandler>(Worksheet_PropertyChanged, 
                    (h) => _worksheet.PropertyChanged -= h).DisposeWith(_worksheet.Disposer);
                _worksheet.PropertyChanged += _propertyChangedHandler;
            }
        }

#endregion

    }
}
